import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
import _slicedToArray from '@babel/runtime/helpers/esm/slicedToArray';
import _toConsumableArray from '@babel/runtime/helpers/esm/toConsumableArray';
import { omit, pick } from '@szzj/utils';
import { Form } from 'antd';
import classnames from 'classnames';
import sortedUniq from 'lodash/sortedUniq';
import React from 'react';
import { ClsPrefix } from '../consts';
import { FieldContextProvider } from '../contexts/FieldContext';
import { useFormContext } from '../contexts/FormContext';
import './index.css';
var _excluded = ['required', 'className', 'children', 'rules', 'when', 'dynamicProps'];
var AntdFormItemPropNames = [
  // antd 属性
  'colon',
  'dependencies',
  'extra',
  'getValueFromEvent',
  'getValueProps',
  'hasFeedback',
  'help',
  'hidden',
  'htmlFor',
  'initialValue',
  'label',
  'labelAlign',
  'labelCol',
  'name',
  'normalize',
  'noStyle',
  'preserve',
  'required',
  'rules',
  'shouldUpdate',
  'tooltip',
  'trigger',
  'validateFirst',
  'validateStatus',
  'validateTrigger',
  'valuePropName',
  'wrapperCol',
  // 自定义 FormItem 属性
  'formItemClassName',
  'formItemStyle',
];
/** 额外的自定义属性 */
export var FormItemCustomPropNames = ['when', 'dynamicProps'];
/**
 * 表单容器
 * 扩展 when 字段用于表单联动
 * @param props
 * @returns
 */
var FormItem = function FormItem(props) {
  var _classnames;
  var form = useFormContext();
  var _props$required = props.required,
    required = _props$required === void 0 ? true : _props$required,
    className = props.className,
    children = props.children,
    _props$rules = props.rules,
    rules = _props$rules === void 0 ? [] : _props$rules,
    when = props.when,
    dynamicProps = props.dynamicProps,
    rest = _objectWithoutProperties(props, _excluded);
  var formItemClassName = classnames(
    ((_classnames = {}),
    _defineProperty(_classnames, ''.concat(ClsPrefix, '-form-item'), true),
    _defineProperty(_classnames, className, !!className),
    _classnames),
  );
  var finalRules = rules;
  if (
    required &&
    props.label &&
    finalRules.every(function (rule) {
      return typeof rule !== 'function' && !rule.required;
    })
  ) {
    finalRules.unshift({
      required: true,
      message: ''.concat(props.label, '\u4E0D\u80FD\u4E3A\u7A7A'),
    });
  }
  if (when || dynamicProps) {
    var _ref = when !== null && when !== void 0 ? when : [],
      _ref2 = _slicedToArray(_ref, 2),
      _ref2$ = _ref2[0],
      predicate =
        _ref2$ === void 0
          ? function () {
              return true;
            }
          : _ref2$,
      _ref2$2 = _ref2[1],
      dependencies = _ref2$2 === void 0 ? [] : _ref2$2;
    var _ref3 = dynamicProps !== null && dynamicProps !== void 0 ? dynamicProps : [],
      _ref4 = _slicedToArray(_ref3, 2),
      getDynamicProps = _ref4[0],
      _ref4$ = _ref4[1],
      getDynamicPropsDeps = _ref4$ === void 0 ? [] : _ref4$;
    return /*#__PURE__*/ React.createElement(
      Form.Item,
      {
        noStyle: true,
        dependencies: sortedUniq(
          [].concat(_toConsumableArray(dependencies), _toConsumableArray(getDynamicPropsDeps)),
        ),
      },
      function (_ref5) {
        var _getDynamicProps;
        var getFieldsValue = _ref5.getFieldsValue;
        var realDynamicProps =
          (_getDynamicProps =
            getDynamicProps === null || getDynamicProps === void 0
              ? void 0
              : getDynamicProps(form)) !== null && _getDynamicProps !== void 0
            ? _getDynamicProps
            : {};
        var dynamicFormItemProps = pick(realDynamicProps, AntdFormItemPropNames);
        var dynamicFieldItemProps = omit(realDynamicProps, AntdFormItemPropNames);
        var vals = getFieldsValue();
        var predicated = predicate(vals, form);
        // 在函数体内渲染 FieldComp 才可以获取到最新的表单数据
        return predicated
          ? /*#__PURE__*/ React.createElement(
              FieldContextProvider,
              {
                value: _objectSpread(
                  _objectSpread({}, props),
                  {},
                  {
                    rules: finalRules,
                  },
                  realDynamicProps,
                ),
              },
              /*#__PURE__*/ React.createElement(
                Form.Item,
                _objectSpread(
                  _objectSpread(
                    {
                      className: formItemClassName,
                      required: required,
                      validateFirst: true,
                    },
                    rest,
                  ),
                  {},
                  {
                    rules: finalRules,
                  },
                  dynamicFormItemProps,
                ),
                /*#__PURE__*/ React.isValidElement(children)
                  ? /*#__PURE__*/ React.cloneElement(children, dynamicFieldItemProps)
                  : children,
              ),
            )
          : null;
      },
    );
  }
  return /*#__PURE__*/ React.createElement(
    FieldContextProvider,
    {
      value: _objectSpread(
        _objectSpread({}, props),
        {},
        {
          rules: finalRules,
        },
      ),
    },
    /*#__PURE__*/ React.createElement(
      Form.Item,
      _objectSpread(
        _objectSpread(
          {
            className: formItemClassName,
            required: required,
            validateFirst: true,
          },
          rest,
        ),
        {},
        {
          rules: finalRules,
        },
      ),
      children,
    ),
  );
};
export default FormItem;
